//
//  XJVisitorView.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/16.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit
import SnapKit

/**
 *  代理协议
 */
protocol XJVisitorViewDelegate:NSObjectProtocol{

    func didClickLoginBtn()
    func didClickRegistBtn()
}

class XJVisitorView: UIView {
    
    weak var delegate: XJVisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     调用代理实现点击事件
     */
    @objc private func didClickLoginBtn(){
    
        delegate?.didClickLoginBtn()
    }
    @objc private func didClickRegistBtn(){
    
        delegate?.didClickRegistBtn()
    }
    
    /**
     提供给外界设置访问视图界面
     */
    func setVisitorInfo(imageName: String?,message: String?){
        
        //在主页设置这两个参数为空,则显示circleview
        if let img = imageName,msg = message {
            
            self.iconView.image = UIImage(named: img)
            self.messageLabel.text = msg
            
        }else{
            setAni()
        }
        
    }
    
    /**
     显示circleview,并开始动画
     */
    private func setAni(){
    
        circleView.hidden = false
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    /**
     布局界面
     */
    private func setupUI(){
        
        backgroundColor = UIColor(white: 236/255.0, alpha: 1)
        
        //添加控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registBtn)
        
        //房子约束
        iconView .snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //圆圈约束
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        //半透明图片约束
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleView)
        }
        
        //messageLabel约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskIconView.snp_bottom).offset(10)
            make.centerX.equalTo(iconView)
            
        }

        
        //登录按钮约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
        }
        
        
        //注册按钮约束
        registBtn.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLabel)
            make.top.width.equalTo(loginBtn)
        }
        
    }
    
    /**
    *  懒加载控件
    */
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var circleView:UIImageView = {
    
        let circleView = UIImageView()
        circleView.hidden = true
        circleView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return circleView
    }()
    //半透明背景
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //消息label
    private lazy var messageLabel:UILabel = {
    
        let label = UILabel (textColor: UIColor.darkGrayColor(), fontSize: 14, preferredMaxLayoutWidth: 245)
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .Center
       
        return label
    }()
    
    //登录按钮
    private lazy var loginBtn:UIButton = {
    
        let btn = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        //添加点击事件
        btn.addTarget(self, action: "didClickLoginBtn", forControlEvents: .TouchUpInside)
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
        return btn
    }()
    
    
    //注册按钮
    private lazy var registBtn:UIButton = {
        
        let btn = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        
        //添加点击事件
        btn.addTarget(self, action: "didClickRegistBtn", forControlEvents: .TouchUpInside)
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
        return btn
        
    }()

}
